import unittest

import bpy

from .utils import new_tag_empty, random_transform, clear_scene, ASSET_PATH
from ..utils.log import log
from ..utils.preferences import Preferences


class TestAssetImport(Preferences,
                      unittest.TestCase,
                      ):

    def setUp(self):
        print('清理场景')
        clear_scene()
        pref = self.pref
        pref.mode = "ASSET"
        if not pref.location_box:
            """没有空物体定位框
            生成一个"""
            new_tag_empty()
        self.pref.asset_root_path = ASSET_PATH

        self.assertEqual(
            bpy.ops.asset.render_master_clear_asset(), {"FINISHED"})
        self.assertEqual(
            bpy.ops.asset.render_master_load_asset(), {"FINISHED"})

    def tearDown(self):
        clear_scene()

    @staticmethod
    def asset_property():
        tf = (True, False)
        import itertools
        emm = []
        for item in itertools.product(tf, tf, tf, tf, tf, tf, tf):
            emm.append(item)
        return emm

    @staticmethod
    def scene_property():
        tf = (True, False)
        emm = []

        import itertools
        for item in itertools.product(tf, tf, tf):
            emm.append(item)
        return emm

    def test_object_import(self):
        for a, b, c, d, e, f, g in self.asset_property():
            pref = self.pref
            obj = pref.pop_object
            pref.import_mode = "PREVIEW" if a else "IMPORT"
            pref.import_link = b
            pref.import_to_active_collect = c
            pref.import_use_fake_user = d
            pref.import_set_parent_form_tag = e
            pref.import_to_location_box = f
            pref.import_to_cursor = g

            random_transform(self.location_box)

            log.debug(f"import object {obj.asset_path}")

            self.assertEqual(
                bpy.ops.asset.render_master_import_asset(
                    asset_path=obj.asset_path,
                    asset_type="OBJECT"),
                {"FINISHED"}
            )

    def test_scene_import(self):
        for a, b, c in self.scene_property():
            pref = self.pref
            pref.import_link = a
            pref.import_use_fake_user = b
            pref.import_whole_scene = c
            scene = pref.pop_scene

            log.debug(f"import scene {scene.asset_path}")

            self.assertEqual(
                bpy.ops.asset.render_master_import_asset(asset_path=scene.asset_path,
                                                         asset_type="SCENE"),
                {"FINISHED"}
            )
